class Solution(object):
    def decodeString(self, s):
        """
        :type s: str
        :rtype: str
        """
        result = []
        q = []
        for c in s:
            if c != "]":
                q.append(c)
            else:
                tmp_c = q.pop()
                sub_str = ""
                while tmp_c != "[":
                    sub_str = tmp_c + sub_str
                    tmp_c = q.pop()
                sub_num = ""
                while q and q[-1] in "0123456789":
                    sub_num = q.pop() + sub_num
                sub_str = int(sub_num) * sub_str
                q.append(sub_str)
        result = "".join(q)
        return result
